﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 存放一些计算工具
/// </summary>
public static class MathTool {
    /// <summary>
    /// 向量对应的绕z轴逆时针旋转的角度，默认多旋转了90度，可以在第二个参数修改
    /// </summary>
    /// <param name="v"></param>
    /// <param name="offset"></param>
    /// <returns></returns>
    public static float GetAngle(Vector2 v, int offset = 90)
    {
        v = v.normalized;
        float arcsin;
        float l = v.magnitude;
        arcsin = Mathf.Asin(Mathf.Abs(v.y / l));
        float angle = arcsin * 180 / Mathf.PI;
        if (v.x < 0 && v.y > 0)
        {
            angle = 180 - angle;
        }
        if (v.x < 0 && v.y < 0)
        {
            angle = 180 + angle;
        }
        if (v.x > 0 && v.y < 0)
        {
            angle = 360 - angle;
        }
        angle += offset;
        return angle;
    }
    /// <summary>
    /// 根据角度获得对应单位向量,以x轴正方向开始
    /// </summary>
    /// <param name="angle"></param>
    /// <returns></returns>
    public static Vector2 GetVector2(float angle)
    {
        float sin = Mathf.Sin(angle * Mathf.PI / 180);
        float cos = Mathf.Cos(angle * Mathf.PI / 180);
        return new Vector2(cos,sin);
    }
    /// <summary>
    /// 获得向量v2相对于v1的对称向量
    /// </summary>
    /// <param name="v1"></param>
    /// <param name="v2"></param>
    /// <returns></returns>
    public static Vector2 GetMirrorVector2(Vector2 v1,Vector2 v2)
    {
        return (2*(v1.x * v2.x + v1.y * v2.y) *v1/(v1.x*v1.x+v1.y*v1.y))-v2;
    }
}
